/*
 * @Author: silas  
 * @Date: 2020-12-18 10:56:32 
 * @Last Modified by: silas
 * @Last Modified time: 2021-04-28 11:29:50
 */

<template>
    <div class="newUpToDownOrganization">
        <div class="contentTopTitle">
            <div class="fl">
                <div class="title">自上而下编制趋势</div>
            </div>
        </div>
        <div>
            <el-form
                :inline="true"
                :model="form"
                label-width="70px"
                class="formList"
            >
                <el-form-item label="预算年度">
                    <el-date-picker
                        v-model="form.year"
                        type="year"
                        placeholder="选择年"
                        value-format="yyyy"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="区域">
                    <dx-cascader
                        :area_id.sync="form.areaId"
                        :area_name.sync="form.areaName"
                        :area_lvl.sync="form.areaLvl"
                        ref="cascader"
                    />
                </el-form-item>
                <el-form-item label="数据来源">
                    <el-select v-model="form.resourceId" placeholder="请选择">
                        <el-option
                            v-for="(item, index) in resourceList"
                            :key="index"
                            :label="item.resourceName"
                            :value="item.resourceId"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item class="btnBox">
                    <div>
                        <el-button size="small" type="primary" @click="search"
                            >查询</el-button
                        >
                    </div>
                      <div>
                        <el-button
                            size="small"
                            class="upload-demo"
                            type="orange"
                            @click="downModal()"
                            >模板下载</el-button
                        >
                    </div>
                    <div>
                        <el-button
                            size="small"
                            type="orange"
                            class="upload-demo"
                            @click="batchExport"
                            >导出</el-button
                        >
                    </div>
                    <!-- <div>
            <el-button size="small" style="margin-left: 10px" @click="exportModal(false)">模版下载</eo" @cll-button>
          </div> -->
                    <el-upload
                        class="upload-demo"
                        action="/api/compileIncome/importData"
                        :limit="3"
                        :data="params"
                        :on-success="importTarget"
                        :on-progress="progress"
                        :on-error="errorUpload"
                        :show-file-list="false"
                    >
                        <el-button size="small" type="orange">导入</el-button>
                    </el-upload>
                  
                </el-form-item>
            </el-form>
        </div>
        <div class="table-content clearfix">
            <div class="content" >
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="总收入表" name="first">
                        <tab1
                            :rowData="tableData"
                            :colData="colDefList"
                            ref="tab1"
                        ></tab1>
                    </el-tab-pane>
                    <el-tab-pane label="主量业务" name="second">
                        <tab2
                            :rowData="tableData"
                            :colDefList="colDefList"
                            ref="tab2"
                        ></tab2>
                    </el-tab-pane>
                    <el-tab-pane label="toBtoC" name="third">
                        <tab3
                            :rowData="tableData"
                            :colDefList="colDefList"
                            ref="tab3"
                        ></tab3>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </div>
    </div>
</template>

<script>
import tab1 from "./trends-tab1";
import tab2 from "./trends-tab2";
import tab3 from "./trends-tab3";
import prefixAPI from "@/api/prefixAPI";
import { getCookie } from "@/common/js/cookie";
import { getSessionStorage } from "@/common/js/storage";
import { $budgetAPI } from "@/api";
export default {
    name: "newUpToDownOrganization",
    components: {
        tab1,
        tab2,
        tab3
    },
    data() {
        return {
            form: {
                year: "",
                areaId: "",
                resourceId: false,
            },
            resourceList: [
                {
                    resourceName: "系统测算",
                    resourceId: false
                },
                {
                    resourceName: "经营单元导入",
                    resourceId: true
                }
            ],
            activeName: "first",
            fileName: "",
            flag: 1,
            accountInfo: "",
            tableData: [],
            colDefList: []
        };
    },
    computed: {
        params() {
            let obj = {
                token: getCookie("token"),
                areaId: this.form.areaId,
                year: this.form.year,
                importFlag: this.form.resourceId,
                flag: this.flag
            };
            return obj;
        },
        areaSelect() {
            return this.form.areaSelect;
        }
    },
    created() {
        // let query = this.$route.query;
        // this.backRecord = typeof query.backRecord === undefined ? true : false;
        // if (this.backRecord) {
          
        // } else {
        //     this.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
        //     this.form.areaId = this.accountInfo.areaId;
        //     this.form.year = this.moment()
        //         .add(1, "years")
        //         .format("YYYY");
        // }
        //   this.form.areaId = query.areaId;
        //     this.form.year = query.accMonth;
        //     this.resourceId = true;
    },
    mounted() {
        var that = this;
        
        that.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
        that.form.areaId = that.accountInfo.areaId;
        that.getYear();
    },
    methods: {
        errorUpload(err, file, fileList) {
            console.log(err);
            this.loading.close();
        },
        progress(event, file, fileList) {
            this.loading = this.$loading({
                lock: true,
                text: "正在导入",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
            });
        },
        //导入成功的回调
        importTarget(response, file, fileList) {
            this.alertError(response);
            this.loading.close();
            if (response.code == 0) {
                this.$message({
                    type: "success",
                    message: "导入成功"
                });
                this.form.resourceId = true;
                this.getData(this.flag);
            }
        },
        //查询
        search() {
            var that = this;
            
            if (this.flag == 1) {
                that.getData(1);
            }
            if (this.flag == 2) {
                that.getData(2);
            }
            if (this.flag == 3) {
                that.getData(3);
            }
            
        },
        //导出
        exportModal() {
            if (this.form.checked == false) {
                let areaId = this.form.areaId;
                let year = this.form.year;
                let importFlag = this.form.resourceId;
                let token = getCookie("token");
                // 根据不同模式切换不同的路径
                if (process.env.NODE_ENV == "development") {
                    window.location.href =
                        prefixAPI.loc +
                        `/api/budgetIncomeSecond/downloadTemplate?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}`;
                } else if (process.env.NODE_ENV == "production") {
                    window.location.href =
                        prefixAPI.hp +
                        `/api/budgetIncomeSecond/downloadTemplate?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}`;
                }
            } else {
                let areaId = this.form.areaId;
                let year = this.form.year;
                let importFlag = this.form.resourceId;
                let token = getCookie("token");
                // 根据不同模式切换不同的路径
                if (process.env.NODE_ENV == "development") {
                    window.location.href =
                        prefixAPI.loc +
                        `/api/budgetIncomeDetail/downloadBudgetIncomeDetail?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}`;
                } else if (process.env.NODE_ENV == "production") {
                    window.location.href =
                        prefixAPI.hp +
                        `/api/budgetIncomeDetail/downloadBudgetIncomeDetail?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}`;
                }
            }
        },
        batchExport() {
             var that = this,
                areaId = that.form.areaId,
                importFlag = that.form.resourceId,
                year = that.form.year,
                token = getCookie('token');

              if (process.env.NODE_ENV == "development") {
                    window.location.href =
                        prefixAPI.loc +
                        `/api/compileIncome/downloadData?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}&flag=0`;
                } else if (process.env.NODE_ENV == "production") {
                    window.location.href =
                        prefixAPI.hp +
                        `/api/compileIncome/downloadData?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}&flag=0`;
                }
        },
        listResourceData(params) {
            
        },
        //数据点击切换页请求不同的数据
        handleClick(tab, event) {
            if (tab.name == "first") {
                this.flag = 1;
                this.getData(1);
            }
            if (tab.name == "second") {
                this.flag = 2;
                this.getData(2);
            }
            if (tab.name == "third") {
                this.flag = 3;
                this.getData(3);
            }
        },
        downModal() {
            var that = this,
                areaId = that.form.areaId,
                importFlag = that.form.resourceId,
                year = that.form.year,
                token = getCookie('token');

              if (process.env.NODE_ENV == "development") {
                    window.location.href =
                        prefixAPI.loc +
                        `/api/compileIncome/downloadTemplate?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}&flag=0`;
                } else if (process.env.NODE_ENV == "production") {
                    window.location.href =
                        prefixAPI.hp +
                        `/api/compileIncome/downloadTemplate?areaId=${areaId}&importFlag=${importFlag}&year=${year}&token=${token}&flag=0`;
                }
            
        },
        // 获取年份
        getYear() {
            $budgetAPI.getBudgetYrear(
                {},
                res => {
                    if (res.code == 0) {
                        this.form.year = res.content;
                        this.getData(1);
                    } else {
                        this.alertError(res);
                    }
                },
                err => {}
            );
        },
        getData(flag) {
            var that = this;
            
            that.$refs['tab' + that.flag].tableLoading1 = true;
            $budgetAPI.getTrentDown(
                {
                    areaId: that.form.areaId,
                    year: that.form.year,
                    importFlag: that.form.resourceId,
                    flag: flag
                }, res => {
                    console.log(res);
                    if(res && res.code == 0) {
                        that.colDefList = res.content.colsList;
                        that.tableData = res.content.data;
                        if (that.flag == 1) {
                            that.$nextTick(() => {
                                that.$refs.tab1.init();
                            });
                        }
                        if (that.flag == 2) {
                            that.$nextTick(() => {
                                that.$refs.tab2.init();
                            });
                        }
                        if (that.flag == 3) {
                            that.$nextTick(() => {
                                that.$refs.tab3.init();
                            });
                        }
                    }
                }
            )

        },
    }
};
</script>

<style lang="scss" scoped>
.newUpToDownOrganization {
    .table-content {
        .table-header {
            display: flex;
            justify-content: space-between;
            .btn-right {
                display: flex;
            }
            .table-title {
                font-size: 14px;
                font-weight: bold;
                height: 30px;
                line-height: 30px;
                span {
                    display: inline-block;
                    width: 5px;
                    height: 5px;
                    background-color: #418ee9;
                    border-radius: 1000%;
                    overflow: hidden;
                    margin-right: 10px;
                    margin-bottom: 3px;
                }
            }
        }
        .content {
            position: relative;
            .fileName {
                position: absolute;
                right: 0;
                top: 15px;
            }
        }
    }
}
</style>
<style lang="scss">
.newUpToDownOrganization {
    .formList {
        .el-form-item {
            width: 21%;
        }
        .el-form-item.btnBox {
            margin-bottom: 0px;
            width: 18%;
            margin-right: 0;
            .el-form-item__content {
                display: flex;
            }
        }
        .el-form-item.checkedBox {
            width: 8%;
        }
        .el-checkbox__inner {
            border-radius: 100%;
        }
    }
    .upload-demo {
        display: inline-block;
        margin-left: 15px;
    }
}
</style>
